﻿using System.Collections.Generic;
using System.ServiceModel;

namespace RockBus.DataContracts
{
    [MessageContract(WrapperNamespace = StringConstants.RockBusNamespace)]
    public class AddOrUpdatePublishersRequest
    {
        private AddOrUpdatePublishersRequest()
        {
            this.Publishers = new List<PublisherInfo>();
        }

        public AddOrUpdatePublishersRequest(params PublisherInfo[] publishers)
            : this((IEnumerable<PublisherInfo>)publishers)
        {
        }

        public AddOrUpdatePublishersRequest(IEnumerable<PublisherInfo> publishers)
            : this()
        {
            ((List<PublisherInfo>)this.Publishers).AddRange(publishers);
        }

        public AddOrUpdatePublishersRequest(PublisherConfiguration publisherConfiguration)
            : this()
        {
            ((List<PublisherInfo>)this.Publishers).AddRange(publisherConfiguration.EvaluationElementDictionary.Values);
        }

        [MessageBodyMember]
        public ICollection<PublisherInfo> Publishers { get; private set; }

        public PublisherConfiguration CreatePublisherConfiguration(PublisherConfiguration toExclude)
        {
            PublisherConfiguration pc = new PublisherConfiguration();

            foreach (var pi in this.Publishers)
            {
                if (!toExclude.EvaluationElementDictionary.ContainsKey(pi.Id) &&
                    !pi.IsStatic)
                {
                    pc.EvaluationElementDictionary.Add(pi.Id, pi);
                }
            }

            return pc;
        }
    }
}